package server;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

public class ServerWindow extends JFrame {
	private JTextField PortField;
	private JLabel lblStatus;
	private AEServer server=null;
	
	public ServerWindow() {
		setLocation(new Point(200, 200));
		setSize(new Dimension(500, 200));
		getContentPane().setLocation(new Point(200, 200));
		getContentPane().setSize(new Dimension(300, 300));
		setTitle("AES Server");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(null);
		
		JLabel lblAesServer = new JLabel("AES Server");
		lblAesServer.setForeground(Color.BLUE);
		lblAesServer.setHorizontalAlignment(SwingConstants.CENTER);
		lblAesServer.setFont(new Font("Tahoma", Font.PLAIN, 20));
		lblAesServer.setBounds(97, 11, 241, 23);
		getContentPane().add(lblAesServer);
		
		JLabel lblPort = new JLabel("Port:");
		lblPort.setBounds(65, 45, 46, 14);
		getContentPane().add(lblPort);
		
		PortField = new JTextField();
		PortField.setBounds(107, 42, 86, 20);
		getContentPane().add(PortField);
		PortField.setColumns(10);
		
		lblStatus = new JLabel("Not Running");
		lblStatus.setForeground(Color.RED);
		lblStatus.setFont(new Font("Tahoma", Font.PLAIN, 20));
		lblStatus.setHorizontalAlignment(SwingConstants.CENTER);
		lblStatus.setBounds(203, 45, 135, 51);
		getContentPane().add(lblStatus);	
		JButton btnStartStop = new JButton("Start / Stop");

		btnStartStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int p = 5555;
				if(server==null){
					if(PortField.getText().length()>0)
						p=Integer.parseInt(PortField.getText());
					server = new AEServer(p);
				}
				if(server.isListening()){
					server.stopListening();
					lblStatus.setText("Not Running");
					lblStatus.setForeground(Color.RED);
				}
				else
					try {
						server.listen();
						lblStatus.setText("Running");
						lblStatus.setForeground(Color.GREEN);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
		});
		btnStartStop.setBounds(65, 73, 128, 23);
		getContentPane().add(btnStartStop);
		
		this.setVisible(true);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ServerWindow sw = new ServerWindow();
	}
}
